/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;


/**
 *
 * @author amega
 */
@Entity
@Table(name="orders", catalog="ors", schema="")
@NamedQueries({
    @NamedQuery(name="Orders.getOrderPrice", query = "SELECT o FROM orders o WHERE o.id = :id_order" ),
    @NamedQuery(name="Orders.getUserOrders", query = "SELECT o FROM orders o WHERE o.id_user = :id_user")
})
public class Orders extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="price")
    private double  price;
    @Temporal(javax.persistence.TemporalType.DATE)
    private String dateTime;
    @ManyToOne
    @JoinColumn(name="id_user", referencedColumnName="id")
    private Users userOrder;
    @OneToMany(mappedBy="id_order")
    private List<Baskets> listBaskets;
    @ManyToMany
    @JoinTable(
            name="tables_orders",
            joinColumns={@JoinColumn(name="id_order",referencedColumnName="id")},
            inverseJoinColumns={@JoinColumn(name="id_table", referencedColumnName="id")})
    private List<Tables> listTables;
    
    
    public Orders() {}
    
    public Orders(long id) {
        this.id = id;
    }
    
    public void setPrice(double  price) {
        this.price = price;
    }
    
    public double getPrice() {
        return this.price;
    }
    
    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    public List<Baskets> getListBaskets() {
        return listBaskets;
    }

    public void setListBaskets(List<Baskets> listBaskets) {
        this.listBaskets = listBaskets;
    }

    public List<Tables> getListTables() {
        return listTables;
    }

    public void setListTables(List<Tables> listTables) {
        this.listTables = listTables;
    }

    public Users getUserOrder() {
        return userOrder;
    }

    public void setUserOrder(Users userOrder) {
        this.userOrder = userOrder;
    }
    
    public void addTables(Tables table) {
        if (null == table) return;
        if (null == this.listTables) {
            this.listTables = new ArrayList<Tables>();
        }
        this.listTables.add(table);
    }
    
    
     public boolean deleteTables(Tables table) {
        if (null != this.listTables && null != table) {
            return this.listTables.remove(table);
        } else {
            return false;
        }
    } 
     
    public void addBaskets(Baskets basket) {
        if (null == basket) return;
        if (null == this.listBaskets) {
            this.listBaskets = new ArrayList<Baskets>();
        }
        this.listBaskets.add(basket);
    }
    
    public boolean deleteBaskets(Baskets basket) {
        if (null != this.listBaskets && null != basket) {
            return this.listBaskets.remove(basket);
        } else {
            return false;
        }
    }
    
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Orders)) {
            return false;
        }
        Orders other = (Orders) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "models.Orders[ id=" + id + " ]";
    }
    
}
